﻿using System;
using System.Configuration;
using Twitter.Data;


namespace Twitter
{
    public static class TwitterMessageLogger
    {
        public static void AddLog(string incomingMessageText, string outgoingMessageText, string processorTypeName, Exception ex, string twitterUser)
        {
            ConnectionStringSettings twitterMessageLogConnectionSettings = ConfigurationManager.ConnectionStrings["FriendsNearbyDB"];
            if (twitterMessageLogConnectionSettings != null)
            {
                try
                {
                    TwitterLogDataContext twitterLogCtx = new TwitterLogDataContext(twitterMessageLogConnectionSettings.ConnectionString);

                    twitterLogCtx.LogTwitterMessages.InsertOnSubmit(
                        new LogTwitterMessage
                        {
                            IncomingMessage = incomingMessageText,
                            OutgoingMessage = outgoingMessageText,
                            Processor = processorTypeName,
                            ExceptionWhileProcessingText = (ex == null) ? null : ex.ToString(),
                            ExceptionWhileProcessingType = (ex == null) ? null : ex.GetType().Name,
                            TimeStamp = DateTime.Now,
                            TwitterScreenName = twitterUser
                        });

                    twitterLogCtx.SubmitChanges();
                }
                catch { }
            }
        }
    }
}
